var EditPageDialog = (function(){    
    /**
     * 构造函数.
     */
    var _EditPageDialog = function(cfg){
        var _this = this;
        this.setProps({            
            size : {
                width : 765,
                height : 220
            }, 
            buttons : [
                {
                    props : 'save|' + lan('dialog.buttons.save'),
                    handler : function($btnObj) {
                        var formValues = ujs.browser.form.getValues(this.getForm()),
                            ueid = formValues['id'];
                        if(!ujs.browser.form.validate(this.getForm())){
                            return false;
                        }                        
                        _this.showMask('Submitting ...');                        
                        page.biz.uploadURLEntity(ueid, _this.getFormData(), function(data){                            
                            bubble.msg((_this.config['relType'] == 'survey' ? 'Survey' : 'Event') + ' Updated!');
                            _this.remove();
                        }, null, function(){
                            _this.hideMask();
                        });
                    }
                }
            ]
        });
        this.addLinkForm();
    }.extend(ujs.ui.Dialog);
    
    /**
     * 初始化表单. 
     */
    _EditPageDialog.prototype.addLinkForm = function(){
        var _this = this;
        this.addDialogContent(URLDialogView.linkForm, function($dialogContent) {  
            $dialogContent.find('.noshare, .s8').remove();      
            
            if(!_this.config['publish']){
                
                $dialogContent.append([
                    '<div class="formItem">',
                        '<label>&nbsp;</label>',
                        '<span><button style="border: solid 1px #CCC;padding: 5px 8px;border-radius: 3px;cursor: pointer;letter-spacing: 1px;" class="editContent">Edit Content</button></span>',
                    '</div>'
                ].join(''));
            }
            
            $dialogContent.find('.editContent').click(function(){
                var cfg = _this.config;
                _this.close();                
                cfg.onEditPage(); 
                return false;
            });
              
            _this.iconObj = $dialogContent.find('#icon').singleImgUploader({
                style : 'innerIcon',
                width : 60,
                height : 60, 
                toolbar : false,
                value : '/ujs/app/im/images/link.png',
                uploadImmediately : false                
            }).data('singleImgUploader');            
            _this.initFormData(_this.config['ueid']);    
        });
    };
   
    //------------------------------------------------------------------------------------------------
    
    /**
     * 初始化表单数据.
     */
    _EditPageDialog.prototype.initFormData = function(urlID){
        var _this = this;
        $.ajax({
            url : '/apis/url/{topicEntityId}'.format({
                topicEntityId : urlID
            }),
            success : ujs.hr(function(msg){                        
                var urlEntity = msg['ue'];                    
                _this.currentEntityID = urlEntity['id'];
                ujs.browser.form.setValues(_this.getForm(), urlEntity);
            })
        });
    };
      
    /**
     * 获取表单数据. 
     */
    _EditPageDialog.prototype.getFormData = function(){  
        //url基础数据.  
        var formValues = ujs.browser.form.getValues(this.getForm()),
            json = {
                url : JSON.stringify({
                    type : this.config['relType'],
                    title : formValues['title']  
                })
            };                    
        //url图标上传.
        var formData = Object.toFormData(json);        
        this.iconObj.fillupImageFiles(formData, [[200,200]]);        
        return formData;
    };    
    
    return _EditPageDialog;    
})();
